//
//  NRWCarShareTemplateManager.m
//  New_Runway
//
//  Created by 邢毅 on 2018/5/29.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarShareTemplateManager.h"
#import "NRWShareTemplateView.h"
#import "NRWCarSharePictureModel.h"
#import "NRWCarListsModel.h"


@implementation NRWCarShareTemplateManager

+ (void)downloadImageWithImageUrlArr:(NSArray<NSString *> *)imageUrlArr completed:(void (^)(NSArray<UIImage *> *))completed {
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:imageUrlArr.count];
    
    dispatch_queue_t downloadQueue = dispatch_queue_create("dowload_image_queue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_semaphore_t sema = dispatch_semaphore_create(imageUrlArr.count);
    
    dispatch_sync(downloadQueue, ^{
        
        for (NSInteger i = 0; i < imageUrlArr.count; i++) {
            
            @autoreleasepool {    
                UIImage *image = [self downloadSingleImageWithUrl:imageUrlArr[i]];
                [arrM addObject:image];
                dispatch_semaphore_signal(sema);
            }
        }
    });
    
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
    if (completed) {
        completed(arrM.copy);
    }
}
+ (void)downloadImageWithImageUrlArray:(nonnull NSArray <NSString *>*)imageUrlArray carBaseModelS:(NSArray<Result *>*)models completed:(void(^)(NSArray<NRWCarSharePictureModel *>* imageModelArr))completed completed1:(void(^)(NSArray <UIImage *>* imageArray))completed1{
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:imageUrlArray.count];
    NSMutableArray *imageArrM = [NSMutableArray arrayWithCapacity:imageUrlArray.count];

    dispatch_queue_t downloadQueue = dispatch_queue_create("dowload_image_queue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_semaphore_t sema = dispatch_semaphore_create(imageUrlArray.count);
    
    dispatch_sync(downloadQueue, ^{
        
        for (NSInteger i = 0; i < imageUrlArray.count; i++) {
            Result *rt = [[Result alloc] init];
            if (models.count == 1) {
                rt = models[0];
            }else{
                rt = models[i];
            }
            NRWCarSharePictureModel *model = [[NRWCarSharePictureModel alloc] init];
            model.isMark = YES;
            model.isTemplate = YES;
            model.imageType = 1;
            model.CarModel = rt;
            UIImage *image = [self downloadSingleImageWithUrl:imageUrlArray[i]];

            if (i>=8) {
                model.isSelected = NO;
            }else{
                model.isSelected = YES;
                [imageArrM addObject:model];
            }
         
            model.picture = image;
            [arrM addObject:model];
            dispatch_semaphore_signal(sema);
        }
    });
    
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
    if (completed) {
        completed(arrM.copy);
    }
    if (completed1) {
        completed1(imageArrM.copy);
    }
 
}

+ (UIImage *)downloadSingleImageWithUrl:(NSString *)url {
    
    NSURL *u = [NSURL URLWithString:url];
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    UIImage *img;
    if ([manager diskImageExistsForURL:u]) {
        img =  [[manager imageCache] imageFromDiskCacheForKey:u.absoluteString];
    } else {//从网络下载图片
        NSData *data = [NSData dataWithContentsOfURL:u];
        img = [UIImage imageWithData:data];
        [[manager imageCache] storeImage:img forKey:u.absoluteString toDisk:YES];
    }
    return img;
}

+ (UIImage *)getViewScreenshots:(UIView *)view {
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(375, 667), NO, 0.0);
    view.layer.drawsAsynchronously = YES;
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}

+ (NSArray<UIImage *> *)getTemplateImageWithCarImage:(NSArray<UIImage *> *)images dataModel:(NSArray<Result *> *)dataModels {
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:dataModels.count];
    
    for (NSInteger i = 0; i < dataModels.count; i++) {
        
        NRWShareTemplateView *view = [[NRWShareTemplateView alloc] init];
        view.carImage = images[i];
        
        if (dataModels.count == 1 && images.count > 1) {//如果为
            view.resultModel = dataModels.firstObject;
        } else {
            view.resultModel = dataModels[i];
        }
    
        [arrM addObject:[self getViewScreenshots:view]];
    }
    
    return arrM.copy;
}

+ (UIImage *)getDrawTagImageArrWithOriginalImage:(UIImage *)originalImage tagImage:(UIImage *)tagImage tagImageRect:(CGRect)tagImageRect {
    
    UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, 1);
    
    [originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
    
    [tagImage drawInRect:tagImageRect];
    
    UIImage *newTagImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newTagImage;
}

+ (NSString *)getShareTextWithCarTitle:(NSString *)carTitle carPrice:(CGFloat)carPrice carLicenceDate:(NSInteger)carLicenceDate carLicenceState:(NSInteger)carLicenceState carMileage:(NSNumber *)carMileage shortUrl:(NSString *)shortUrl carCount:(NSInteger)carCount {
    
    if (carCount == 1) {
        return [NSString stringWithFormat:@"【宣传标题】: %@\n【零售价】: %@\n【上牌时间】: %@\n【行驶里程】: %@\n【车辆链接】: %@\n【联系电话】: %@", carTitle, [NSString getCarPrice:carPrice], [NSString getCarLicenceDate:carLicenceDate carLicenceState:carLicenceState], [NSString getCarMileage:carMileage], shortUrl, [NRWCarUser sharedUser].userMobile];
    } else {
        return [NSString stringWithFormat:@"【宣传标题】: 好车超低价, 质量好, 放心买\n【联系电话】: %@", [NRWCarUser sharedUser].userMobile];
    }
}

@end
